package com.design.marketPrj.application.service;

import com.design.marketPrj.application.bean.PO.User;
import com.design.marketPrj.application.bean.PO.UserExample;
import com.design.marketPrj.application.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public User getUserById(String id){
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    public User getUser(String username){
        User user = null;
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> userList = userMapper.selectByExample(userExample);
        if(userList!=null && userList.size()!=0)
            return userList.get(0);
        else
            return null;
    }

    public boolean insert(User user){
        int code = userMapper.insertSelective(user);
        if(code == 1)
            return true;
        else
            return false;
    }

    public boolean update(User user){
        int code = userMapper.updateByPrimaryKey(user);
        if(code == 1)
            return true;
        else
            return false;
    }

    public long countUser(String username){
        UserExample userExample = new UserExample();
        userExample.or().andUsernameEqualTo(username);
        long count = userMapper.countByExample(userExample);
        return count;
    }


}
